//package com.ophiux.auth.config;
//
//import com.ophiux.auth.support.core.CustomeOAuth2TokenCustomizer;
//import com.ophiux.auth.support.core.OphiuxDaoAuthenticationProvider;
//import com.ophiux.auth.support.password.OAuth2ResourceOwnerPasswordAuthenticationProvider;
//import com.ophiux.auth.support.sms.CustomeOAuth2AccessTokenGenerator;
//import com.ophiux.auth.support.sms.OAuth2ResourceOwnerSmsAuthenticationProvider;
//import lombok.RequiredArgsConstructor;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
//import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.builders.WebSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.crypto.password.NoOpPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
//import org.springframework.security.oauth2.server.authorization.token.DelegatingOAuth2TokenGenerator;
//import org.springframework.security.oauth2.server.authorization.token.OAuth2RefreshTokenGenerator;
//import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator;
//import org.springframework.security.provisioning.InMemoryUserDetailsManager;
//import org.springframework.security.web.SecurityFilterChain;
//
///**
// * @desc:
// * 以前我们自定义类继承自 WebSecurityConfigurerAdapter 来配置我们的 Spring Security，我们主要是配置两个东西：
// * configure(HttpSecurity)
// * configure(WebSecurity)
// * 前者主要是配置 Spring Security 中的过滤器链，后者则主要是配置一些路径放行规则。
// *
// * 现在在 WebSecurityConfigurerAdapter 的注释中，人家已经把意思说的很明白了：
// * 1.以后如果想要配置过滤器链，可以通过自定义 SecurityFilterChain Bean 来实现。
// * 2.以后如果想要配置 WebSecurity，可以通过 WebSecurityCustomizer Bean 来实现。
// * @author: hhl
// * @date：2023/6/29 14:11
// */
//@Configuration
//@EnableWebSecurity  // 使用@EnableWebSecurity注解开启Spring Security功能
//@RequiredArgsConstructor
//public class SecurityConfig {
//
////    @Bean
////    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
////        http
////            // 配置授权请求规则
////            .authorizeRequests()
////            // 任何请求都需要认证
////            .anyRequest()
////            .authenticated()
////            // 使用and()方法连接多个配置
////            .and()
////            // 开启HTTP基本认证功能
////            .httpBasic();
////        return http.build();
////    }
//
//
////    private final JwtAthFilter jwtAthFilter;
//
//    // 定义一个 SecurityFilterChain bean，用于配置安全过滤器链
//    @Bean
//    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
////        List<Filter> filters = new ArrayList<>();
////        return new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**"), filters);
//
//        http.authorizeRequests()
//                .anyRequest().authenticated()
//                .and()
//                .formLogin().loginPage("/token/login")
//                .permitAll()
//                .and()
//                .csrf().disable()
////                .userDetailsService(userDetailsService())
//        ;
//        return http.build();
//    }
//
//    // 配置 AuthenticationProvider bean
//    @Bean
//    public AuthenticationProvider authenticationProvider() {
//        final DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
//        authenticationProvider.setUserDetailsService(userDetailsService());
//        authenticationProvider.setPasswordEncoder(passwordEncoder());
//        return authenticationProvider;
//    }
//
//    // 配置 AuthenticationManager bean
//    @Bean
//    public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
//        return config.getAuthenticationManager();
//    }
//
//    // 配置密码编码器 bean
//    @Bean
//    public PasswordEncoder passwordEncoder() {
////        return new BCryptPasswordEncoder();
//        return NoOpPasswordEncoder.getInstance();
//    }
//
//    // 配置 UserDetailsService bean
//    @Bean
//    public UserDetailsService userDetailsService() {
//        InMemoryUserDetailsManager users = new InMemoryUserDetailsManager();
//        users.createUser(User.withUsername("hhl").password("123").roles("admin").build());
//        return users;
////        return new UserDetailsService() {
////            @Override
////            public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
////                return userDao.findUserByEmail(email);
////            }
////        };
//    }
//
//    @Bean
//    public WebSecurityCustomizer webSecurityCustomizer() {
////        return web -> web.ignoring().antMatchers("/api/auth/login","/login","/token/login");
//        return new WebSecurityCustomizer() {
//            @Override
//            public void customize(WebSecurity web) {
//                web.ignoring().antMatchers("/api/auth/login","/login","/token/login");
//            }
//        };
//    }
//
//    /**
//     * 注入授权模式实现提供方
//     *
//     * 1. 密码模式 </br>
//     * 2. 短信登录 </br>
//     *
//     */
//    @SuppressWarnings("unchecked")
//    private void addCustomOAuth2GrantAuthenticationProvider(HttpSecurity http) {
//        AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
//        OAuth2AuthorizationService authorizationService = http.getSharedObject(OAuth2AuthorizationService.class);
//
//        OAuth2ResourceOwnerPasswordAuthenticationProvider resourceOwnerPasswordAuthenticationProvider = new OAuth2ResourceOwnerPasswordAuthenticationProvider(
//                authenticationManager, authorizationService, oAuth2TokenGenerator());
//
//        OAuth2ResourceOwnerSmsAuthenticationProvider resourceOwnerSmsAuthenticationProvider = new OAuth2ResourceOwnerSmsAuthenticationProvider(
//                authenticationManager, authorizationService, oAuth2TokenGenerator());
//
//        // 处理 UsernamePasswordAuthenticationToken
//        http.authenticationProvider(new OphiuxDaoAuthenticationProvider());
//        // 处理 OAuth2ResourceOwnerPasswordAuthenticationToken
//        http.authenticationProvider(resourceOwnerPasswordAuthenticationProvider);
//        // 处理 OAuth2ResourceOwnerSmsAuthenticationToken
//        http.authenticationProvider(resourceOwnerSmsAuthenticationProvider);
//    }
//
//
//    /**
//     * 令牌生成规则实现 </br>
//     * client:username:uuid
//     * @return OAuth2TokenGenerator
//     */
//    @Bean
//    public OAuth2TokenGenerator oAuth2TokenGenerator() {
//        CustomeOAuth2AccessTokenGenerator accessTokenGenerator = new CustomeOAuth2AccessTokenGenerator();
//        // 注入Token 增加关联用户信息
//        accessTokenGenerator.setAccessTokenCustomizer(new CustomeOAuth2TokenCustomizer());
//        return new DelegatingOAuth2TokenGenerator(accessTokenGenerator, new OAuth2RefreshTokenGenerator());
//    }
//
//}